package 周赛;

import org.junit.Test;

import java.util.Arrays;

public class 完成任务的最小工作时间段 {

    public int minSessions(int[] tasks, int sessionTime) {
        Arrays.sort(tasks);
        int i=0;
        int j=tasks.length-1;
        int num=0;
        int curr=sessionTime;
        while (i<=j){
            int last = curr - tasks[j];
            j--;
            while (last-tasks[i]>=0 && i<=j){
                last=last-tasks[i];
                i++;
            }
            num++;
        }
        return num==0?1:num;
    }
    @Test
    public void test(){
        int[] tasks={1,5,7,10,3,8,4,2,6,2};
        int i = minSessions(tasks, 10);
    }

//    public int minSessions(int[] tasks, int sessionTime) {
//        int num=0;
//        Arrays.sort(tasks);
//        int curr=sessionTime;
//        for (int i = 0; i < tasks.length-1; i++) {
//            int last = curr - tasks[i];
//            if (last>=tasks[i+1]) {
//                curr=last;
//            }
//            else {
//                num+=1;
//                curr=sessionTime;
//            }
//        }
//        return (curr==sessionTime || num==0) ? num+1 : num;
//    }

}
